package com.xzx.study.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/4/23 21:01
 */
public class StudentProvider extends ContentProvider {
    private static final String TAG = "TAG-" + StudentProvider.class.getSimpleName();
    private static final int OPERATE_NONE_ID_CODE = 1;
    private static final int OPERATE_ID_CODE = 2;
    // 存放合法uri的容器
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        //content://com.xzx.study.provider.studentprovider/student
        //content://com.xzx.study.provider.studentprovider/student/3
        uriMatcher.addURI("com.xzx.study.provider.studentprovider", "/student", OPERATE_NONE_ID_CODE);
        // #代表任意数字
        uriMatcher.addURI("com.xzx.study.provider.studentprovider", "/student/#", OPERATE_ID_CODE);
    }

    private StudentDbHelper studentDbHelper;

    // 一旦注册就会创建
    public StudentProvider() {
        Log.i(TAG, "StudentProvider");
    }
    //一旦注册就会创建，    provider对象创建后调用
    @Override
    public boolean onCreate() {
        Log.i(TAG, "StudentProvider onCreate");
        studentDbHelper = new StudentDbHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri,String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase database = studentDbHelper.getReadableDatabase();
        int match = uriMatcher.match(uri);
        if (match == OPERATE_ID_CODE) {
            Log.i(TAG, "根据ID查询");
            long id = ContentUris.parseId(uri);
            return database.query("student", projection, "_id=?", new String[]{id+""}, null,null,null);
        } else if (match == OPERATE_NONE_ID_CODE) {
            Log.i(TAG, "不根据id查询");
            return database.query("student", projection, selection, selectionArgs, null, null, sortOrder);
        } else {
            throw new RuntimeException("不支持查询的uri");
        }
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int match = uriMatcher.match(uri);
        if (match == OPERATE_NONE_ID_CODE) {
            SQLiteDatabase database = studentDbHelper.getReadableDatabase();
            long id = database.insert("student", null, values);
            database.close();
            return ContentUris.withAppendedId(uri, id);
        } else {
            throw new RuntimeException("不支持插入的uri");
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int match = uriMatcher.match(uri);
        if (match == OPERATE_ID_CODE) {
            long id = ContentUris.parseId(uri);
            SQLiteDatabase database = studentDbHelper.getReadableDatabase();
            int count = database.delete("student", "_id=?", new String[]{id+""});
            database.close();
            return count;
        } else {
            throw new RuntimeException("不支持删除的uri");
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int match = uriMatcher.match(uri);
        if (match == OPERATE_ID_CODE) {
            long id = ContentUris.parseId(uri);
            SQLiteDatabase database = studentDbHelper.getReadableDatabase();
            int count = database.update("student", values, "_id=?", new String[]{id+""});
            database.close();
            return count;
        } else {
            throw new RuntimeException("不支持修改的uri");
        }
    }
}
